package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.query.UserQuery;
import com.tarena.lbs.user.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class AdminRepositoryImpl implements AdminRepository {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public AdminPO getAdminByPhone(String phone) {
        QueryWrapper<AdminPO> query = new QueryWrapper<>();
        query.eq("account_phone",phone);
        return adminMapper.selectOne(query);
    }

    @Override
    public AdminPO getAdminById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminPO> pagesList(AdminQuery query) {
        QueryWrapper<AdminPO> queryWrapper = assembleAdminQuery(query);
        //PageHelper.startPage 之间不能有代码
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<AdminPO> pos = adminMapper.selectList(queryWrapper);
        //selectList 数据库操作 之间不能有代码
        return new PageInfo<>(pos);
    }

    @Override
    public void save(AdminPO adminPO) {
        adminMapper.insert(adminPO);
    }

    @Override
    public void deleteById(Integer id) {
        AdminPO adminPO = new AdminPO();
        adminPO.setAccountStatus(2);
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        adminMapper.update(adminPO,queryWrapper);
    }

    @Override
    public void resetPassword(Integer id, AdminPO adminPO) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        adminMapper.update(adminPO,queryWrapper);
    }

    @Override
    public void updateById(AdminPO adminPO) {
        log.info("updateById:{}",adminPO);
        adminMapper.updateById(adminPO);
    }

    private QueryWrapper<AdminPO> assembleAdminQuery(AdminQuery query) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        // 自动排除已逻辑删除的数据
        //账号状态不等于2
        queryWrapper.ne("account_status",2);
        // 添加明确的升序排序规则，确保按id从小到大显示
        queryWrapper.orderByDesc("id");
        //1.accountPhone 账号手机号 相等查询 where account_phone=#{phone}
        if (StringUtils.isNotBlank(query.getAccountPhone())){
            queryWrapper.eq("account_phone",query.getAccountPhone());
        }
        //2.nickname 昵称 模糊查询 非空 where nickname like(%#{}%)
        if (StringUtils.isNotBlank(query.getNickname())){
            queryWrapper.like("nickname",query.getNickname());
        }
        //3.accountType 用户类型 相等查询
        if (query.getAccountType() != null){
            queryWrapper.eq("account_type",query.getAccountType());
        }
        //4.id 主键ID 相等查询
        if (query.getId() != null){
            queryWrapper.eq("id",query.getId());
        }
        return queryWrapper;
    }

    /*
     * 查询用户数量
     * */
    @Override
    public Long selectUserCount(UserQuery query) {
        return userMapper.selectUserCount(query);
    }

    /*
     * 批量查询用户
     * */
    @Override
    public List<UserVO> selectUserList(UserQuery query) {
        return userMapper.selectUserList(query);
    }

    @Override
    public UserVO selectUserById(Integer id) {
        UserPO po = userMapper.selectById(id);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(po,userVO);
        return userVO;
    }
}
